package com.ymatou.iapi.pushbind.testcase;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.GetAllBindClientBean;
import com.ymatou.iapi.pushbind.service.GetAllBindClientCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PushCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
*获取所有绑定客户端
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetAllBindClient{
	private static GetAllBindClientBean getallbindclientBean;
	private static GetAllBindClientCall getallbindclientCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取所有绑定客户端");
	}

	@Before
	public void caseUp() {
		getallbindclientBean=new GetAllBindClientBean();
		getallbindclientCall=new GetAllBindClientCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getallbindclientCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAllBindClient_001() {
		Logger.start(true,"获取所有绑定客户端");
		try {
			int userId = new Random().nextInt(10000);
			String clientId = "Test" + YMTDateUtil.getDate();
			BindUserClientBean bindUserClientBean = PushCallService.intitUserClient(userId);
			PushCallService.bindUserClient(bindUserClientBean);
			getallbindclientBean.setUserId(userId);
			getallbindclientBean.setUserSystemType(bindUserClientBean.getUserSystemType());
			getallbindclientCall.setData(getallbindclientBean);
			getallbindclientCall.callService();
			String clientIds = getallbindclientCall.getJsonObject().get("Result").getAsJsonObject().get("clientIds").getAsJsonArray().toString();
		//	Logger.debug(clientIds);

			Logger.comment("验证数据库的数据");
			Thread.sleep(2000);
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBindBydeciveId(bindUserClientBean.getDeviceId()).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(sqlMap.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(bindUserClientBean.getDeviceId(),sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getZrange(userId,"BindRecord:UserBindSet:"+userId);
			Logger.debug(set);
			String str = StringUtils.join(set.toArray(), ";");
			Logger.verifyEquals(true,clientIds.equals(str),"验证clientIds");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAllBindClient_002() {
		Logger.start(true,"获取所有绑定客户端,多设备");
		try {
			int userId = new Random().nextInt(10000);;
			String deviceId1 = "xiaomage"+ YMTDateUtil.getBeforeOrNextSecond(1);
			String deviceId2 = "xiaomage123"+ YMTDateUtil.getBeforeOrNextSecond(3);
			String clientId1 = "Test123" + YMTDateUtil.getDate();
			String clientId2 = "Test" + YMTDateUtil.getDate();

			BindUserClientBean bindUserClientBean1 = PushCallService.intitUserClient(userId);
			BindUserClientBean bindUserClientBean2 = PushCallService.intitUserClient(userId);
			bindUserClientBean1.setDeviceId(deviceId1);
			bindUserClientBean1.setClientId(clientId1);
			bindUserClientBean2.setDeviceId(deviceId2);
			bindUserClientBean2.setClientId(clientId2);
			PushCallService.bindUserClient(bindUserClientBean1);
			PushCallService.bindUserClient(bindUserClientBean2);
			getallbindclientBean.setUserId(userId);
			getallbindclientBean.setUserSystemType(bindUserClientBean1.getUserSystemType());
			getallbindclientCall.setData(getallbindclientBean);
			getallbindclientCall.callService();
			String clientIds = getallbindclientCall.getclientIds().toString();
			Logger.debug(clientIds);
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap2 = imDefaultWapper.selectPushBindBydeciveId(bindUserClientBean2.getDeviceId()).get(0);

			Logger.verifyEquals(userId,Integer.parseInt(sqlMap2.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(bindUserClientBean2.getDeviceId(),sqlMap2.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId2,sqlMap2.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getZrange(userId,"BindRecord:UserBindSet:"+userId);
			String str = StringUtils.join(set.toArray(), ";");
			//Logger.verifyEquals(true,clientIds.equals(str),"验证redis");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}